package org.rule;


import cn.hutool.core.bean.BeanUtil;
import com.googlecode.aviator.AviatorEvaluator;
import lombok.Data;
import org.rule.domain.dto.RuleExecDTO;
import org.rule.exec.RuleExec;
import org.rule.function.ListInFunction;
import org.rule.function.NumEqualFunction;
import org.rule.function.ObjectNotNullFunction;
import org.rule.function.StrEqualFunction;
import org.rule.function.StrNotEqualFunction;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws Exception {
        Sex s = new Sex();
        s.sex = "1";
        Person p = new Person();
        p.age=1;
        p.setSex(s);
//        JSONObject jb = JSONObject.from(p);
//        Map map  = JSONObject.parseObject(jb.toJSONString(), Map.class);
        Map<String, Object> map = BeanUtil.beanToMap(p);
        List<String> seg = List.of("0","2");
        System.out.println(seg);
        AviatorEvaluator.addFunction(new NumEqualFunction());
        AviatorEvaluator.addFunction(new ObjectNotNullFunction());
        AviatorEvaluator.addFunction(new StrEqualFunction());
        AviatorEvaluator.addFunction(new StrNotEqualFunction());
        AviatorEvaluator.addFunction(new ListInFunction());
        String list = "0,1,2";
        map.put("list", list);
        Object r2 =  AviatorEvaluator.execute("list_in(sex.sex, list)", map);
        System.out.println(r2);
        map.put("num1", new BigDecimal(100L));
        map.put("num2", "100.0");

        long r =  (Long)AviatorEvaluator.execute("num_equal(num1, num2)", map);
        System.out.println(r);
        map.put("person", new Person());
        boolean b = (boolean) AviatorEvaluator.execute("obj_not_null(person)", map);
        System.out.println(b);
        // 测试
        // name = 1 ,  sex= man 的返回 success
        // name = 1 ,  sex = nv 返回 success
        // name = 2 ,  sex = nv 返回 success

        RuleExecDTO start = RuleExecDTO.builder()
                .ruleId("personRuleId")
                .nodeId("rule_000")
                .nodeDsc("start")
                .nodeTpCd("0")
                .fstParam("name")
                .operator("STR_EQ")
                .secParam("1")
                .successToNextNode("rule_001")
                .failedToNextNode("rule_002")
                .execResult("")
                .build();
        RuleExecDTO r_001 = RuleExecDTO.builder()
                .ruleId("personRuleId")
                .nodeId("rule_001")
                .nodeDsc("r1")
                .nodeTpCd("1")
                .fstParam("sex.sex")
                .operator("STR_EQ")
                .secParam("nv")
                .successToNextNode("rule_003")
                .failedToNextNode("rule_002")
                .execResult("")
                .build();

        RuleExecDTO r_002 = RuleExecDTO.builder()
                .ruleId("personRuleId")
                .nodeId("rule_002")
                .nodeDsc("r1")
                .nodeTpCd("9")
                .fstParam("")
                .operator("")
                .secParam("")
                .successToNextNode("")
                .failedToNextNode("")
                .execResult("failed")
                .build();

        RuleExecDTO r_003 = RuleExecDTO.builder()
                .ruleId("personRuleId")
                .nodeId("rule_003")
                .nodeDsc("r2")
                .nodeTpCd("9")
                .fstParam("")
                .operator("")
                .secParam("")
                .successToNextNode("")
                .failedToNextNode("")
                .execResult("success")
                .build();

        List<RuleExecDTO> ruleList = new ArrayList<>();
        ruleList.add(start);
        ruleList.add(r_001);
        ruleList.add(r_002);
        ruleList.add(r_003);
        Person pp = new Person();
        pp.name="1";
        pp.age = 20;
        Sex ss = new Sex();
        ss.sex = "man";
        pp.sex = ss;
        String o = RuleExec.execute(ruleList, pp);
        System.out.println("规则执行结果--->");
        System.out.println(o);
    }

    @Data
    public static class Person {

        private String name;
        private Integer age;
        private Sex sex;
    }
    @Data
    public static class Sex{
        private String sex;
    }
}